from Case.python_ui.Common.basepage import Basepage
import pytest, time
from Case.python_ui.Pageobject.user_product_waf import UserProductWaf
from Case.python_ui.PageLocate.user_my_resource_locator import UserMyResourceLocator
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from Case.python_ui.PageLocate.user_product_cms_locator import ProductCmsLocator
from Case.python_ui.PageLocate.user_product_waf_locator import ProductWafLocator
from Case.python_ui.Pageobject.login_pages import Loginpages
from Case.python_ui.Test_data.login_data import Logindata
from selenium import webdriver


class UserProductCms(Basepage):

    # 点击左上角菜单按钮进入菜单页面
    def enter_menu_page(self):
        doc = '点击左上角菜单按钮进入菜单页面'
        self.wait_elevisble(ProductCmsLocator.product_menu_button, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.product_menu_button, doc)
        time.sleep(3)
        self.wait_elevisble(ProductCmsLocator.product_menu_page, doc)

    # 点击菜单cms按钮进入CloudMonitor控制台overview页面
    def enter_cms_overview_page(self):
        doc = '点击菜单cms按钮进入CloudMonitor控制台overview页面'
        time.sleep(6)
        for i in range(3):
            try:
                self.scroll_bar_operation(ProductCmsLocator.cms_menu_button)
                self.wait_elevisble(ProductCmsLocator.cms_menu_button, doc)
                time.sleep(2)
                self.move_mouse_to_click_element(ProductCmsLocator.cms_menu_button, doc)
                break
            except:
                self.refresh_page()
                time.sleep(3)
                self.enter_menu_page()
                continue
        time.sleep(8)
        # 检测cms控制台页面信息
        self.driver.switch_to.frame(self.get_element(ProductCmsLocator.into_iframe, doc))
        self.wait_elevisble(ProductCmsLocator.cms_overview_title, doc)
        self.save_screen(doc)

    # 检测cms控制台页面信息
    def check_cms_overview_page(self):
        doc = '检测cms控制台页面信息'
        time.sleep(2)
        overv = self.get_element(ProductCmsLocator.cms_overview_title, doc).text
        assert overv == 'Overview'
        monovw = self.get_element(ProductCmsLocator.cms_monitoring_overview_label, doc).text
        assert monovw == 'Monitoring overview'
        alertru = self.get_element(ProductCmsLocator.cms_alert_rules_title, doc).text
        assert alertru == 'Alert Rules'
        eventalert = self.get_element(ProductCmsLocator.cms_event_triggered_alert_rules_title, doc).text
        assert eventalert == 'Event-triggered Alert Rules'
        alerthistory = self.get_element(ProductCmsLocator.cms_alert_history_title, doc).text
        assert alerthistory == 'Alert History'
        # 滑动页面
        # self.scroll_bar_operation(ProductCmsLocator.cms_dashboards_title)
        dashhs = self.get_element(ProductCmsLocator.cms_dashboards_title, doc).text
        assert dashhs == 'Dashboards'

    # 进入Cloud Service Monitoring页面
    def enter_cms_csm_page(self):
        doc = '进入csm页面'
        time.sleep(2)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_csm_title, doc)
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_csm_page, doc)
        self.wait_elevisble(ProductCmsLocator.cms_ecs_title_btn, doc)
        ecs_text = self.get_element(ProductCmsLocator.cms_ecs_title_btn, doc).text
        assert ecs_text == 'Elastic Compute Service (ECS)'

    # 点击Elastic Compute Service (ECS)标题进入ECS页面
    def enter_cms_ecs_page(self):
        doc = '点击Elastic Compute Service (ECS)标题进入ECS页面'
        time.sleep(2)
        # 点击Elastic Compute Service (ECS)标题进入ECS页面
        self.move_mouse_to_click_element(ProductCmsLocator.cms_ecs_title_btn, doc)
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_ecs_example_apge, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_ecs_example_btn, doc)
        self.wait_elevisble(ProductCmsLocator.cms_csm_btn_box, doc)

    # 检测ECS页面信息
    def check_cms_ecs_page(self):
        doc = '检测ECS页面信息'
        instance_name = self.get_element(ProductCmsLocator.cms_ecs_instance_name_btn, doc).text
        assert instance_name == 'Instance Name'
        instance_id = self.get_element(ProductCmsLocator.cms_csm_instance_id_btn, doc).text
        assert instance_id == 'Instance ID'
        intranet_ip = self.get_element(ProductCmsLocator.cms_csm_network_ip_btn, doc).text
        assert intranet_ip == 'Classic Network Intranet IP'
        address_ip = self.get_element(ProductCmsLocator.cms_csm_address_ip_btn, doc).text
        assert address_ip == 'Elastic IP Address'
        statust = self.get_element(ProductCmsLocator.cms_csm_status_btn, doc).text
        assert statust == 'Status'

    # 进入ECS监控图表界面
    def enter_ecs_monitoring_page(self):
        doc = '进入ECS监控图表界面'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.ecs_monitoring_charts_btn, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.ecs_monitoring_charts_btn, doc)
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.ecs_monitoring_charts_page, doc)

    # 检测ECS监控图表界面信息
    def check_ecs_monitoring_page(self):
        doc = '检测ECS监控图表界面信息'
        time.sleep(3)
        os_monitoring = self.get_element(ProductCmsLocator.ecs_os_monitoring_btn, doc).text
        assert os_monitoring == 'OS Monitoring'
        cpu_title = self.get_element(ProductCmsLocator.ecs_cpu_title, doc).text
        assert cpu_title == 'CPU utilization(%)'
        memory_title = self.get_element(ProductCmsLocator.ecs_memory_title, doc).text
        assert memory_title == 'Memory usage(%)'
        system_title = self.get_element(ProductCmsLocator.ecs_system_title, doc).text
        assert system_title == 'System load(value)'
        diskusage_title = self.get_element(ProductCmsLocator.ecs_diskusage_title, doc).text
        assert diskusage_title == 'DiskUsage(%)'
        num_read_title = self.get_element(ProductCmsLocator.ecs_num_read_title, doc).text
        assert num_read_title == 'Number of bytes read(bytes/s)'
        num_written_title = self.get_element(ProductCmsLocator.ecs_num_written_title, doc).text
        assert num_written_title == 'Number of bytes written(bytes/s)'

    # 进入RDS监控界面
    def enter_rds_monitoring_page(self):
        doc = '进入RDS监控界面'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_db_tab, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_db_tab, doc)
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_rds_title, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_rds_title, doc)
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_rds_example_title, doc)

    # 检测RDS监控界面信息
    def check_rds_monitoring_page(self):
        doc = '检测RDS监控界面信息'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_rds_rules_btn, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_rds_rules_btn, doc)
        rds_instance_name = self.get_element(ProductCmsLocator.cms_rds_instance_name_btn, doc).text
        assert rds_instance_name == 'Instance Name'
        rds_instance_id = self.get_element(ProductCmsLocator.cms_rds_instance_id_btn, doc).text
        assert rds_instance_id == 'Instance ID'

    # 进入SLB监控界面
    def enter_slb_monitoring_pages(self):
        doc = '进入SLB监控界面'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_network_tab, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_network_tab, doc)
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_slb_title, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_slb_title, doc)
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_slb_example_page, doc)

    # 检测SLB监控界面信息
    def check_slb_monitoring_page(self):
        doc = '进入SLB监控界面'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_slb_rules_btn, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_slb_rules_btn, doc)
        time.sleep(2)
        self.save_screen(doc)
        slb_instance_name = self.get_element(ProductCmsLocator.cms_slb_instance_name_btn, doc).text
        assert slb_instance_name == 'Instance Name'
        slb_instance_id = self.get_element(ProductCmsLocator.cms_slb_instance_id_btn, doc).text
        assert slb_instance_id == 'Instance ID'
        slb_ip_address = self.get_element(ProductCmsLocator.cms_slb_ip_address_btn, doc).text
        assert slb_ip_address == 'Ip Address'

    # 进入SLB监控图表界面
    def enter_slb_monitoring_carts_page(self):
        doc = '进入SLB监控图表界面'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.slb_monitoring_charts_btn, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.slb_monitoring_charts_btn, doc)
        time.sleep(5)
        self.wait_elevisble(ProductCmsLocator.slb_monitoring_charts_page, doc)

    # 检测SLB监控图表界面信息
    def check_slb_monitoring_carts_page(self):
        doc = '检测ECS监控图表界面信息'
        time.sleep(3)
        custom_title_btn = self.get_element(ProductCmsLocator.slb_custom_title_btn, doc).text
        assert custom_title_btn == 'Custom'
        inbound_bndwidth_title = self.get_element(ProductCmsLocator.slb_inbound_bndwidth_title, doc).text
        assert inbound_bndwidth_title == 'Inbound Bandwidth(bits/s)'
        outbound_bandwidths_title = self.get_element(ProductCmsLocator.slb_outbound_bandwidths_title, doc).text
        assert outbound_bandwidths_title == 'Outbound Bandwidth(bits/s)'
        sum_inflow_bandwidth_title = self.get_element(ProductCmsLocator.slb_sum_inflow_bandwidth_title, doc).text
        assert sum_inflow_bandwidth_title == 'Sum of inflow bandwidth(bits/s)'
        sum_outflow_bandwidth_title = self.get_element(ProductCmsLocator.slb_sum_outflow_bandwidth_title, doc).text
        assert sum_outflow_bandwidth_title == 'Sum of outflow bandwidth(bits/s)'
        num_new_connections_title = self.get_element(ProductCmsLocator.slb_num_new_connections_title, doc).text
        assert num_new_connections_title == 'Number of New Connections(Count)'
        num_inbound_data_title = self.get_element(ProductCmsLocator.slb_num_inbound_data_title, doc).text
        assert num_inbound_data_title == 'Number of Inbound Data Packets(Count/s)'
        self.scroll_bar_operation(ProductCmsLocator.slb_monitoring_charts_page)
        num_outbound_data_title = self.get_element(ProductCmsLocator.slb_num_outbound_data_title, doc).text
        assert num_outbound_data_title == 'Number of Outbound Data Packets(Count/s)'

    # 进入OSS监控界面
    def enter_oss_monitoring_pages(self):
        doc = '进入OSS监控界面'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_storage_tab, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_storage_tab, doc)
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_oss_title, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_oss_title, doc)
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_oss_example_page, doc)

    # 检测OSS监控界面信息
    def check_oss_monitoring_page(self):
        doc = '进入OSS监控界面'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_oss_rules_btn, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_oss_rules_btn, doc)
        time.sleep(2)
        oss_bucket_name_btn = self.get_element(ProductCmsLocator.cms_oss_bucket_name_btn, doc).text
        assert oss_bucket_name_btn == 'Bucket Name'
        cms_oss_page_title = self.get_element(ProductCmsLocator.cms_oss_page, doc).text
        assert cms_oss_page_title == 'Object Storage Service (OSS)'

    # 进入OSS监控图表界面
    def enter_oss_monitoring_carts_page(self):
        doc = '进入OSS监控图表界面'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.oss_monitoring_charts_btn, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.oss_monitoring_charts_btn, doc)
        time.sleep(5)
        self.wait_elevisble(ProductCmsLocator.oss_monitoring_charts_page, doc)

    # 检测OSS监控图表界面信息
    def check_oss_monitoring_carts_page(self):
        doc = '检测OSS监控图表界面信息'
        time.sleep(3)
        custom_title_btn = self.get_element(ProductCmsLocator.oss_custom_title_btn, doc).text
        assert custom_title_btn == 'Custom'
        valid_request_title = self.get_element(ProductCmsLocator.oss_valid_request_title, doc).text
        assert valid_request_title == 'Availability/Valid Request Rate(%)(%)'
        valid_requests_title = self.get_element(ProductCmsLocator.oss_valid_requests_title, doc).text
        assert valid_requests_title == 'Total Number of Requests/Number of Valid Requests(Count)'
        traffic_title = self.get_element(ProductCmsLocator.oss_traffic_title, doc).text
        assert traffic_title == 'Traffic(byte)'
        meteringstorageutilization_title = self.get_element(ProductCmsLocator.oss_meteringstorageutilization_title, doc).text
        assert meteringstorageutilization_title == 'MeteringStorageUtilization(byte)'
        meteringstorageutilizationGB_title = self.get_element(ProductCmsLocator.oss_meteringstorageutilizationGB_title, doc).text
        assert meteringstorageutilizationGB_title == 'MeteringStorageUtilizationGB(Gbytes)'
        storage_usage_title = self.get_element(ProductCmsLocator.oss_storage_usage_title, doc).text
        assert storage_usage_title == 'Storage Usage(%)'

    # 进入VPN Gateways监控界面
    def enter_vpn_gateways_pages(self):
        doc = '进入VPN Gateways监控界面'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_network_tab, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_network_tab, doc)
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_vpn_gw_title, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_vpn_gw_title, doc)
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_vpn_gw_example_page, doc)

    # 检测VPN Gateways监控界面信息
    def check_vpn_gateways_pages(self):
        doc = '检测VPN Gateways监控界面信息'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.cms_vpn_gw_rules_btn, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.cms_vpn_gw_rules_btn, doc)
        time.sleep(2)
        vpn_gw_instance_id_btn = self.get_element(ProductCmsLocator.cms_vpn_gw_instance_id_btn, doc).text
        assert vpn_gw_instance_id_btn == 'Instance ID'

    # 进入VPN Gateways监控图表界面
    def enter_vpn_gateways_monitoring_carts_page(self):
        doc = '进入VPN Gateways监控图表界面'
        time.sleep(2)
        self.wait_elevisble(ProductCmsLocator.vpn_gateways_monitoring_charts_btn, doc)
        self.move_mouse_to_click_element(ProductCmsLocator.vpn_gateways_monitoring_charts_btn, doc)
        time.sleep(5)
        self.wait_elevisble(ProductCmsLocator.vpn_gateways_monitoring_charts_page, doc)

    # 检测VPN Gateways监控图表界面信息
    def check_vpn_gateways_monitoring_carts_page(self):
        doc = '检测VPN Gateways监控图表界面信息'
        time.sleep(3)
        custom_title_btn = self.get_element(ProductCmsLocator.vpn_gateways_custom_title_btn, doc).text
        assert custom_title_btn == 'Custom'
        rxpkgs_title = self.get_element(ProductCmsLocator.vpn_gatewayss_rxpkgs_title, doc).text
        assert rxpkgs_title == 'IPSec.connection.rxPkgs(pps)'
        txpkgs_title = self.get_element(ProductCmsLocator.vpn_gateways_txpkgs_title, doc).text
        assert txpkgs_title == 'IPSec.connection.txpkgs(pps)'
        rx_rate_title = self.get_element(ProductCmsLocator.vpn_gatewayss_rx_rate_title, doc).text
        assert rx_rate_title == 'IPSec.connection.rx.rate(bits/s)'
        tx_rate_title = self.get_element(ProductCmsLocator.vpn_gateways_tx_rate_title, doc).text
        assert tx_rate_title == 'IPSec.connection.tx.rate(bits/s)'
        ssl_client_title = self.get_element(ProductCmsLocator.vpn_gatewayss_ssl_client_title, doc).text
        assert ssl_client_title == 'SSL Client Count(unit)'




